package com.hz.zbl.controller;

import com.alibaba.fastjson.JSON;
import com.hz.zbl.mapper.UserMapper;
import com.hz.zbl.model.request.ApiResponse;
import com.hz.zbl.service.UserManagerService;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserLoginController {

    @Resource
    UserManagerService userManagerService;

    @Resource
    UserMapper userMapper;

    @PostMapping("/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        @RequestParam("user_type") Integer user_type){

        String res = userManagerService.login(username,password,user_type);

//        return res == JSON.toJSONString(userMapper.selectByUnameAndUtype(username,user_type)) ? ApiResponse.ok(res) : ApiResponse.userLogin(res);

        return res == null ? ApiResponse.intError() : ApiResponse.ok(res);
    }



    @PostMapping("/logout")
    public String logout(@RequestParam("username") String username){
        int result = userManagerService.logout(username);
        return result > 0 ? ApiResponse.ok(result) : ApiResponse.intError();
    }


    @PostMapping("/change-pwd")
    public String changePwd(@RequestParam("id") Integer id,
                            @RequestParam("orignPwd") String orignPwd,
                            @RequestParam("newPwd") String newPwd,
                            @RequestParam("newPwdConfirm") String newPwdConfirm){
        int result = userManagerService.changePwd(id, orignPwd, newPwd, newPwdConfirm);
        System.out.println(result);
        return result > 3 ? ApiResponse.ok(result) : ApiResponse.intError();
    }

}
